package com.gengzp.common.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.gengzp.common.model.user.dto.UserOnlineInfoDto;

/**
 * @ClassName UserInfoContextHolder
 * @Description 用户信息线程上下文
 * @Author gengzp
 * @Date 2025/5/11 3:54
 */
public class UserInfoContextHolder {

    private static final ThreadLocal<UserOnlineInfoDto> userInfoThreadLocal = new TransmittableThreadLocal<>();

    /**
     * 获取当前用户信息
     */
    public static UserOnlineInfoDto getCurrentUserInfo() {
        return userInfoThreadLocal.get();
    }

    /**
     * 设置当前用户信息
     */
    public static void setCurrentUserInfo(UserOnlineInfoDto userOnlineInfoDto) {
        userInfoThreadLocal.set(userOnlineInfoDto);
    }

    /**
     * 清理当前线程变量中的用户信息
     */
    public static void clear() {
        userInfoThreadLocal.remove();
    }

}
